/**
 * @Author: 陈瑞鹏
 * @Date:   2022-10-24 19:30:30
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-10-26 10:10:21
 */
import abiao_utils from 'abiao_utils';
import express from 'express';
import { equipApi } from '../config/api.config';
import equip from '../controllers/equip.controller';
import item from '../controllers/item.controller';
import reward from '../controllers/reward.controller';
import user from '../controllers/user.contorller';
import { Code } from '../enum/code.enum';
import { HANDLE_TYPE, PROP_NAME, FUNC_NAME } from '../enum/itemLogs.enum';
import Equip from '../models/equip/equip.model';
import Hero from '../models/hero/hero.model';
import Retain from '../models/stats/retain.model';
import Redis from '../utils/redis';
import Result from '../utils/result';
import utils from '../utils/utils';
const equipRoute = express.Router();
/**
* @api {post} /equip/wearEquip 穿戴装备
* @apiGroup 装备
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} id 武将ID
* @apiParam {Number} equipId 装备唯一ID _id
* @apiName wearEquip
* @apiVersion 1.0.0
*/
equipRoute.post(equipApi.wearEquip, async (req, res) => {
  let { roleId, id, equipId } = req.body;

  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');

  let EquipRes: any = await Equip.findById(equipId);
  if (!EquipRes) throw new Result(Code.Fail, '装备错误');

  if (EquipRes.isWear) throw new Result(Code.Fail, '该装备已穿戴');

  // if (HeroRes.level < EquipRes.useLv) throw new Result(Code.Fail, '当前武将未达到穿戴等级');
  // 替换装备
  if (HeroRes.equipment[EquipRes.part] != '' && HeroRes.equipment[EquipRes.part] != 0) {
    await Equip.updateOne({ _id: HeroRes.equipment[EquipRes.part] }, { isWear: false });
  }

  HeroRes.equipment[EquipRes.part] = EquipRes._id;

  await Hero.updateOne({ roleId, id }, { equipment: HeroRes.equipment });

  await Equip.updateOne({ _id: equipId }, { isWear: true });

  HeroRes = await Hero.find({ roleId });

  EquipRes = await Equip.find({ roleId });

  let retData = {
    HeroRes,
    EquipRes,
  }

  return res.json({ code: Code.Succ, message: "穿戴成功!", data: retData });
})

/**
* @api {post} /equip/oneClickWearEquip 一键穿戴装备
* @apiGroup 装备
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} id 武将ID
* @apiName oneClickWearEquip
* @apiVersion 1.0.0
*/
equipRoute.post(equipApi.oneClickWearEquip, async (req, res) => {
  let { roleId, id } = req.body;

  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');

  let EquipRes: any = await Equip.find({ roleId, useLv: { $lte: HeroRes.level }, isWear: false }).sort({ useLv: -1 });
  for (const key in EquipRes) {
    if (HeroRes.equipment[EquipRes[key].part] == '' || HeroRes.equipment[EquipRes[key].part] == '0') {
      HeroRes.equipment[EquipRes[key].part] = EquipRes[key]._id;
    }
  }


  for (const iterator of HeroRes.equipment) {
    if (iterator != '0' && iterator != '') {
      await Equip.updateOne({ _id: iterator }, { isWear: true });
    }
  }

  await Hero.updateOne({ roleId, id }, { equipment: HeroRes.equipment });

  HeroRes = await Hero.find({ roleId });

  EquipRes = await Equip.find({ roleId });

  let retData = {
    HeroRes,
    EquipRes,
  }

  return res.json({ code: Code.Succ, message: "一键穿戴成功!", data: retData });
})
/**
* @api {post} /equip/oneClickRemoveEquip 一键卸下装备
* @apiGroup 装备
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} id 武将ID
* @apiName oneClickRemoveEquip
* @apiVersion 1.0.0
*/
equipRoute.post(equipApi.oneClickRemoveEquip, async (req, res) => {
  let { roleId, id } = req.body;


  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');

  if (HeroRes.equipment.length == 0) {
    throw new Result(Code.Fail, '无装备可卸下');
  }

  for (const iterator of HeroRes.equipment) {
    await Equip.updateOne({ _id: iterator }, { isWear: false });
  }

  await Hero.updateOne({ roleId, id }, { equipment: ['0', '0', '0', '0', '0', '0'] })

  HeroRes = await Hero.find({ roleId });

  let EquipRes = await Equip.find({ roleId });

  let retData = {
    HeroRes,
    EquipRes
  }

  return res.json({ code: Code.Succ, message: "一键卸下成功!", data: retData });
})



/**
* @api {post} /equip/removeEquip 卸下装备
* @apiGroup 装备
* @apiParam {Number} roleId 角色ID
* @apiParam {Number} id 武将ID
* @apiParam {Number} equipId 装备唯一ID _id
* @apiName removeEquip
* @apiVersion 1.0.0
*/
equipRoute.post(equipApi.removeEquip, async (req, res) => {
  let { roleId, id, equipId } = req.body;

  let HeroRes: any = await Hero.findOne({ roleId, id });
  if (!HeroRes) throw new Result(Code.Fail, '未拥有该武将');

  let EquipRes: any = await Equip.findById({ _id: equipId });
  if (!EquipRes) throw new Result(Code.Fail, '装备错误');

  if (!EquipRes.isWear) throw new Result(Code.Fail, '当前装备未穿戴');

  let data: any;
  for (const key in HeroRes.equipment) {
    if (HeroRes.equipment[key] == equipId) {
      data = HeroRes.equipment[key];
      // HeroRes.equipment.splice(key, 1);
      HeroRes.equipment[key] = '0'
    }
  }

  if (!data) throw new Result(Code.Fail, '找不到装备');

  await Hero.updateOne({ roleId, id }, { equipment: HeroRes.equipment });

  await Equip.updateOne({ _id: equipId }, { isWear: false });

  HeroRes = await Hero.find({ roleId });

  EquipRes = await Equip.find({ roleId });

  let retData = {
    HeroRes,
    EquipRes
  }

  return res.json({ code: Code.Succ, message: "卸下成功!", data: retData });
})


/**
* @api {post} /equip/batchDecEquip 批量分解装备
* @apiGroup 装备
* @apiDescription 批量分解装备
* @apiParam {Number} roleId 角色ID
* @apiParam {String} decId 需要分解的装备唯一ID  [_id,_id,_id]
* @apiName batchDecEquip
* @apiVersion 1.0.0
*/
equipRoute.post(equipApi.batchDecEquip, async (req, res) => {
  let { roleId, decId } = req.body;

  if (decId.length == 0) throw new Result(Code.Fail, '参数错误');
  let EquipRes: any = null;
  for (const iterator of decId) {
    EquipRes = await Equip.findOne({ _id: iterator });
    if (!EquipRes) throw new Result(Code.Fail, '装备不存在');

    reward.sendDiamond(roleId, 1, "装备出售返还1钻石", FUNC_NAME.EQUIP_FJ)
    await Equip.deleteOne({ _id: iterator });
  }

  let userInfo = await user.getUserInfo(roleId);
  EquipRes = await Equip.find({ roleId });
  let retData = {
    userInfo,
    EquipRes
  }
  return res.json({ code: Code.Succ, message: "出售成功!", data: retData });

})

/**
* @api {post} /equip/qhEquip 强化装备
* @apiGroup 装备
* @apiDescription 强化装备
* @apiParam {Number} roleId 角色ID
* @apiParam {String} _id  装备唯一ID
* @apiName qhEquip
* @apiVersion 1.0.0
*/
equipRoute.post(equipApi.qhEquip, async (req, res) => {
  let { roleId, _id } = req.body;

  let EquipRes: any = await Equip.findById({ _id });
  if (!EquipRes) throw new Result(Code.Fail, '装备不存在');

  let qhLv = EquipRes.qhLv + 1;
  if (qhLv > EquipRes.maxQhLv) throw new Result(Code.Fail, '已到达最高强化等级');

  let goldCoin = EquipRes.qhLv * EquipRes.useLv * 100;

  switch (EquipRes.attributeType) {
    case 4: case 5:
      goldCoin *= 3;
      break;
  }
  let desc = `强化装备 ${EquipRes.qhLv}=>${qhLv}`;
  await reward.reduceGoldCoin(roleId, goldCoin, desc, FUNC_NAME.EQUIP_QH);

  await Equip.updateOne({ _id }, { qhLv });

  let userInfo = await user.getUserInfo(roleId);
  EquipRes = await Equip.find({ roleId });
  let retData = {
    userInfo,
    EquipRes
  }
  return res.json({ code: Code.Succ, message: "强化成功!", data: retData });

})
export default equipRoute;
